package php.code.instructions;

import com.google.common.base.Joiner;
import php.code.PhpDoc;
import php.code.indentation.Indentable;

public abstract class Instruction extends Indentable {
    private final PhpDoc doc;

    protected Instruction(Indentable parent, boolean generatePhpDoc) {
        super(parent);
        this.doc = generatePhpDoc ? new PhpDoc(this) : null;
    }

    public Instruction(boolean generatePhpDoc) {
        this.doc = generatePhpDoc ? new PhpDoc(this) : null;
    }

    @Override
    public final String toString() {
        return Joiner
                .on("\n")
                .skipNulls()
                .join(
                        doc,
                        getCode()
                );
    }

    protected abstract String getCode();

    public Instruction doc(String... text) {
        for (String line : text)
            doc.content.createLiteral(line);

        return this;
    }
}
